/**
 * 
 */
package model.staff;
import com.opensymphony.xwork2.ActionSupport;
import java.io.*;
import java.sql.*;
import java.util.*;


import myutil.Connect;
import vo.mixed.*;
import vo.*;


public class DocumentMo{
	ArrayList<String> attachments=new ArrayList<String>();
	int attachment_id=0;
	public ArrayList<String> getAttachments() {
		return attachments;
	}

	public void setAttachments(ArrayList<String> attachments) {
		this.attachments = attachments;
	}
	
	
	public ArrayList<DocumentListVo> getDocumentList(int course_professor_id){
		ArrayList<DocumentListVo> list = new ArrayList<DocumentListVo>();
		//HttpSession session;
		//session = ServletActionContext.getRequest().getSession();
		//int id=(int) session.getAttribute("user_id");
	
		System.out.println("id is in :"+course_professor_id);
			Connect c=new Connect();
			String query;
			query="SELECT du.document_id,subject, document_name, upload_date,comment FROM course_document cd, document_upload du WHERE course_professor_id ="+course_professor_id + " AND cd.document_id = du.document_id" ;
			System.out.println(query);
			try {
			ResultSet rs=c.getResult(query);
			while(rs.next())
			{
				int attachment_id,attachment_count=0;String document_name;
				DocumentListVo vo=new DocumentListVo();
				vo.setSubject(rs.getString("subject"));
				document_name=rs.getString("document_name");
				vo.setUpload_date(rs.getInt("upload_date"));
				vo.setComment(rs.getString("comment"));
				attachment_id=rs.getInt("document_id");
				File f=new File("F:/Metalink/attachments/"+attachment_id+"/");
				if(attachment_id!=0)
					attachment_count=f.list().length;
				vo.setAttachment_count(attachment_count);
				vo.setDocument_name(document_name);
				vo.setCourse_professor_id(course_professor_id);
				attachments.add(document_name);
				vo.setAttachment_id(attachment_id);
				//read.add(vo);
				list.add(vo);
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	
	
		
		return list;
	}
	
	public void DeleteDocument(int attachment_id,int course_professor_id){
	Connect c=new Connect();
	String query;
	query="delete from document_upload where document_id="+attachment_id ;
	System.out.println(query);
	try {
		c.dml(query);
	} catch (Exception e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	}
	
	query="delete from course_document where document_id="+attachment_id;
	System.out.println(query);
	try {
		c.dml(query);
	} catch (Exception e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	}
  
		}
	
	
	
	public ArrayList<DocumentListVo> getDocumentCourseDetails(int course_professor_id){
		ArrayList<DocumentListVo> doclist = new ArrayList<DocumentListVo>();
		System.out.println("id is in :"+course_professor_id);
		Connect c=new Connect();
		String query;
		query="SELECT course_name, course_code, lecture_duration, department_name, batch_name FROM course_professor cp, course_details cd, department_details dd, batch_details bd WHERE course_professor_id ="+ course_professor_id+" AND cp.course_id = cd.course_id AND bd.batch_id = cp.batch_id AND cd.department_id = dd.department_id" ;
		System.out.println(query);
		ResultSet rs;
		try {
			rs = c.getResult(query);
			while(rs.next()){
				DocumentListVo vo=new DocumentListVo();
				vo.setCourse_name(rs.getString("course_name"));
				vo.setCourse_code(rs.getString("course_code"));
				vo.setLecture_duration(rs.getInt("lecture_duration"));
				vo.setDepartment_name(rs.getString("department_name"));
				vo.setBatch_name(rs.getString("batch_name"));
				vo.setCourse_professor_id(course_professor_id);
				doclist.add(vo);
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	
		return doclist;
	
	}
}